//
//  CarouselFigureView.m
//  UIImageView
//
//  Created by WangZe on 15/11/26.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "CarouselFigureView.h"
#import "UIImageView+WebCache.h"
#import "BaseImageView.h"

@implementation CarouselFigureView
// 这个视图继承于UIVIew 在他的初始化方法中, 给他从外部传递一个装载着图片的数组
- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray arrayWithArray:array];
        // 创建UIScrollView添加到当前视图上
        [self createView];
    }
    return self;
}
- (void)createView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollView.pagingEnabled = YES;
    
    if (self.array.count) {
        
        
        [self.array insertObject:[self.array lastObject] atIndex:0];
        [self.array addObject:self.array[1]];
        
        for (NSInteger i = 0; i < self.array.count ; i++) {
            // 这个地方用BaseImageView创建 使他的图片具备点击方法
            BaseImageView *imageView = [[BaseImageView alloc] init];
            imageView.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height);
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.array[i]] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
            imageView.tag = 1000 + i - 1;
            imageView.delegeta = self;
//            imageView.image = [UIImage imageNamed:self.array[i]];
            [self.scrollView addSubview:imageView];
//            // 保持图片的比例不变填充满屏幕
//            imageView.contentMode = UIViewContentModeScaleAspectFill;
//            // 超越边界不显示
//            imageView.clipsToBounds = YES;
        }
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.array.count, 0);
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        
       
    }
    [self addSubview:self.scrollView];
    // 设置代理人
    self.scrollView.delegate = self;
    // 创建一个pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 200, self.frame.size.height - 50, 200, 40)];
    [self addSubview:self.page];
    self.page.numberOfPages = self.array.count - 2;
    // 非选中点的颜色
    self.page.pageIndicatorTintColor = [UIColor orangeColor];
    // 当前点的颜色
    self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
    flag = 1;
    // 时间控制器
    self.myTimer =[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    self.scrollView.showsHorizontalScrollIndicator = NO;
}

- (void)pushNextViewController:(NSInteger)tag {
    
    NSLog(@"%ld", tag);
    [self.delegate pushVC:tag];
    
}


// 轮播图自动滚动方法
- (void)timerAction
{
    
    self.scrollView.contentOffset = CGPointMake(SCOLLERWIDTH * flag, 0);
    if (self.scrollView.contentOffset.x == (self.array.count - 1) * SCOLLERWIDTH) {
        self.scrollView.contentOffset = CGPointMake(SCOLLERWIDTH, 0);
        flag = 1;
        
    }
    self.page.currentPage = self.scrollView.contentOffset.x / SCOLLERWIDTH - 1;
    flag++;
    
    
}

// 滑动开始他会触发的协议方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.myTimer invalidate];
}
// 滑动结束他会触发的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    if (self.scrollView.contentOffset.x == (self.array.count - 1) * SCOLLERWIDTH) {
        self.scrollView.contentOffset = CGPointMake(SCOLLERWIDTH, 0);
        
    }
    if (self.scrollView.contentOffset.x == 0)
    {
        self.scrollView.contentOffset = CGPointMake((self.array.count - 2) * SCOLLERWIDTH, 0);
    }
    
    self.page.currentPage = self.scrollView.contentOffset.x / SCOLLERWIDTH - 1;
    flag = self.scrollView.contentOffset.x / SCOLLERWIDTH;
    self.myTimer =[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
